//
//  JCStatusCell.swift
//  SinaWeibo_18
//
//  Created by mac on 15/10/17.
//  Copyright © 2015年 cz. All rights reserved.
//

import UIKit

class JCStatusCell: UITableViewCell {

    var statusFrame: JCStatusFrame?{
        didSet{
            setStatusValue()
        }
    }
    // MARK: - 添加子控件
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        contentView.backgroundColor = RGB(r: 240, g: 240, b: 240)
        
        //添加原创微博的控件
        contentView.addSubview(originalStatusView)
        
        //添加转发微博的控件
        contentView.addSubview(retweetedView)
        
        //添加底部的tabbar
        contentView.addSubview(statusToolBar)
        
    }

    
    // MARK: - 设置子控件的大小和值
    private func setStatusValue(){
        let statusF = statusFrame!
        let status = statusF.status!
        
        //设置原创微博整体View的frame
        originalStatusView.frame = statusF.originalViewF!

        //头像
        headImageView.frame = statusF.headImageViewF!
        headImageView.sd_setImageWithURL(NSURL(string: status.user!.profile_image_url!), placeholderImage: UIImage(named: "avatar_default_big"))
        //名称
        nameLabel.frame = statusF.nameLabelF!
        nameLabel.text = status.user?.name
        
        //会员
        if status.user!.isVip {
            vipImageView.hidden = false
            vipImageView.frame = statusF.vipImageViewF!
            vipImageView.image = UIImage(named: "common_icon_membership_level\(status.user!.mbrank)")
            //显示橘色
            nameLabel.textColor = UIColor.orangeColor()
        }else{
            //不是vip
            vipImageView.hidden = true
            nameLabel.textColor = UIColor.blackColor()
        }
        
        //创建时间
        let creatAt = status.createdAtStr
        createdTimeLabel.frame = statusF.createdTimeLabelF!
        //实时计算label的宽度
        createdTimeLabel.width = creatAt.size(createdTimeLabel.font).width
        createdTimeLabel.text = creatAt
        
        //来源
        sourceLabel.frame = statusF.sourceLabelF!
        sourceLabel.text = status.source
        //原创微博内容
        originalTextLabel.frame = statusF.originalTextLabelF!
        originalTextLabel.text = status.text
        
        // 判断如果有  原创微博配图
        if let originalPicUrls = status.pic_urls where originalPicUrls.count > 0 {
            originalPhotos.hidden = false
            //设置配图内容和frame
            originalPhotos.frame = statusF.originalPhotosF!
            originalPhotos.pic_urls = originalPicUrls
            
        }else{
            //隐藏 配图
            originalPhotos.hidden = true
        }
        
        
        //转发微博内容
        if let retStatus = status.retweeted_status{
            retweetedView.hidden = false
            
            //转发微博整体大小
            retweetedView.frame = statusF.retweetedViewF!
            
            //转发微博内容大小 和值
            retweetedTextLabel.frame = statusF.retweetedTextLabelF!
            retweetedTextLabel.text = retStatus.text
            
            //如果有转发微博配图
            if let retweetedPicUrls = retStatus.pic_urls where retweetedPicUrls.count > 0{
                retweedPhotos.hidden = false
                //转发配图的 frame和值
                retweedPhotos.frame = statusF.retweetedPhotosF!
                retweedPhotos.pic_urls = retweetedPicUrls
            }else{
                retweedPhotos.hidden = true
            }
            
        }else{
            //没有转发微博
            retweetedView.hidden = true
        }
        
        //底部的toolbar
        statusToolBar.frame = statusF.statusToolBarF!
        statusToolBar.status = status
    }
    
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    //原创微博的整体View
    private lazy var originalStatusView: UIView = {
        let view = UIView()
        view.backgroundColor = UIColor.whiteColor()
        //添加子控件
        view.addSubview(self.headImageView)
        view.addSubview(self.nameLabel)
        view.addSubview(self.createdTimeLabel)
        view.addSubview(self.sourceLabel)
        view.addSubview(self.originalTextLabel)
        view.addSubview(self.originalPhotos)
        view.addSubview(self.vipImageView)
        
        return view
    }()
    //头像
    private lazy var headImageView: UIImageView = {
        let imageView = UIImageView()
        imageView.layer.borderWidth = 0.5
        imageView.layer.borderColor = UIColor.lightGrayColor().CGColor
        imageView.layer.masksToBounds = true
        return imageView
    }()
    //名称
    private lazy var nameLabel: UILabel = {
        let nameLabel = UILabel()
        nameLabel.font = UIFont.systemFontOfSize(CELL_STATUS_NAME_FONT)
        return nameLabel
    }()
    //会员图标
    private lazy var vipImageView: UIImageView = UIImageView()
    
    //创建时间
    private lazy var createdTimeLabel: UILabel = {
        let timeLabel = UILabel()
        timeLabel.font = UIFont.systemFontOfSize(CELL_STATUS_CREATEDTIME_FONT)
        timeLabel.textColor = UIColor.darkGrayColor()
        return timeLabel
    }()
    //来源
    private lazy var sourceLabel: UILabel = {
        let label = UILabel()
        label.font = UIFont.systemFontOfSize(CELL_STATUS_CREATEDTIME_FONT)
        label.textColor = UIColor.darkGrayColor()
        return label
    }()
    //原创微博内容
    private lazy var originalTextLabel: UILabel = {
        let label = UILabel()
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(CELL_STATUS_TEXT_FONT)
        return label
    }()
    //原创微博配图
    private lazy var originalPhotos: JCStatusPhotos = JCStatusPhotos()
    
    //转发微博 整体view
    private lazy var retweetedView: UIView = {
        let view = UIView()
        view.backgroundColor = RGB(r: 244, g: 244, b: 244)
        view.addSubview(self.retweetedTextLabel)
        view.addSubview(self.retweedPhotos)
        return view
        }()
    //转发微博内容
    private lazy var retweetedTextLabel: UILabel = {
            let label = UILabel()
        label.numberOfLines = 0
        label.font = UIFont.systemFontOfSize(CELL_STATUS_TEXT_FONT)
        
        return label
        }()
    //转发微博配图
    private lazy var retweedPhotos: JCStatusPhotos = JCStatusPhotos()
    
    
    //底部的工具条
    private lazy var statusToolBar: JCStatusToolBar = JCStatusToolBar()
}
